package com.fastadmin.api;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.fastadmin.common.limit.Limit;
import com.fastadmin.common.limit.LimitType;
import com.fastadmin.config.security.annotation.AnonymousAccess;
import com.fastadmin.controller.BaseController;
import com.fastadmin.mapper.impl.GongGaoMapper;
import com.fastadmin.pojo.GongGao;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@RequiredArgsConstructor
@Slf4j
public class publicApiController extends BaseController {

    public final GongGaoMapper gongGaoMapper;
    public static final String url = "https://m.217dan.com/modules/217Dan_Date_Salary_Cal/DoGetAJAX.php";

    @RequestMapping(value = "/f/api/tools", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    @AnonymousAccess
    public Object getGongGaoById(HttpServletRequest request) {
        Map<String, String> paramMap = ServletUtil.getParamMap(request);
        Map map = binder2.fromJson(binder2.toJson(paramMap), Map.class);
        HttpResponse response = HttpUtil.createPost(url).form(map).execute();
        if (response.isOk()) {
            Map resultMap = binder2.fromJson(StrUtil.utf8Str(response.body()), Map.class);
            return resultMap;
        }else{
            return error();
        }
    }
    @RequestMapping(value = "api/getGongGaoContentById", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object getGongGaoContentById(@RequestParam String id) {
        GongGao byId = gongGaoMapper.findById(id);
        return byId.getContent();
    }

}
